use prettytable::{Attr, Cell, Row, Table};

/// 生成首页 html 代码
pub fn provide_welcome_html(app: &crate::App) -> String {
    let mut table = Table::new();

    table.add_row(Row::new(vec![
        Cell::new(&app.table_heads[0]).with_style(Attr::Bold),
        Cell::new(&app.table_heads[3]).with_style(Attr::Bold),
        Cell::new(&app.table_heads[1]).with_style(Attr::Bold),
        Cell::new(&app.table_heads[2]).with_style(Attr::Bold),
    ]));

    for s in &app.services {
        table.add_row(Row::new(vec![
            Cell::new(&s.name),
            Cell::new(&s.file),
            Cell::new(&s.url),
            Cell::new(&s.desp),
        ]));
    }

    table.printstd();
    println!("");

    let welcome = app
        .services
        .iter()
        .map(|s| {
            format!(
                "      <tr>
                <td>{0}</td>
                <td><a href='{1}'>{1}</a></td>
                <td>{2}</td>
            </tr>",
                s.name, s.url, s.desp
            )
        })
        .collect::<Vec<String>>()
        .join("\n");

    format!(
        "<!DOCTYPE html>
<html>
    <head>
        <meta charset='utf-8'>
        <meta name=viewport content='width=device-width,initial-scale=1'>
        <title>{0}</title>
        <style>
            body {{
                overflow-x: hidden;
            }}
            h1,
            h3 {{
                text-align: center;
            }}
            table,
            table tr th,
            table tr td {{
                border: 1px solid black;
                padding: 0.5em;
            }}
            table {{
                text-align: center;
                margin: auto;
                width: 60%;
                margin-top: 20px;
                border-collapse: collapse;
            }}
            footer {{
                position: absolute;
                text-align: center;
                bottom: 15px;
                padding: 0;
                margin: 0;
                width: 100%;
            }}
        </style>
    </head>
    <body>
        <h1>{0}</h1>
        <h3>{1}</h3> 
        <table>
        <tr>
            <th>{3}</th>
            <th>{4}</th>
            <th>{5}</th>
        </tr>
        {2}
        </table>
        <footer>FQLServer &copy; 2020-2022</footer>
    </body>
</html>
",
        app.title,
        app.sub_title,
        welcome,
        app.table_heads[0],
        app.table_heads[1],
        app.table_heads[2]
    )
}
